package com.ljf.common.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljf.common.enums.ResultCodeEnum;
import com.ljf.common.result.ServiceResultModel;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * service返回数据构建工具类
 *
 * @author LJF
 * @since 2024-01-26
 */
public class ServiceResultUtil {

    private ServiceResultUtil() {

    }

    /**
     * 成功返回
     *
     * @param info
     * @param msg
     * @param rows
     * @param total
     * @return
     * @param <T>
     */
    public static<T extends Serializable> ServiceResultModel<T> success(String info, String msg, List<T> rows, Long total) {
        ServiceResultModel<T> model = new ServiceResultModel<>();
        model.setCode(ResultCodeEnum.OK.getCode());
        model.setInfo(info);
        model.setMsg(msg);
        model.setTimestamp(System.currentTimeMillis());
        model.setRows(rows);
        model.setTotal(total);
        return model;
    }

    /**
     * 查询成功
     *
     * @param page
     * @return
     */
    public static<T extends Serializable> ServiceResultModel<T> success(Page<T> page) {
        List<T> rows = page == null ? Collections.emptyList() : page.getRecords();
        Long total = page == null ? 0L : page.getTotal();
        return success("查询成功", "查询成功", rows, total);
    }

    /**
     * 查询成功
     *
     * @param list
     * @return
     */
    public static<T extends Serializable> ServiceResultModel<T> success(List<T> list) {
        return success(list, "查询成功");
    }

    /**
     * 查询成功
     *
     * @param list
     * @return
     */
    public static<T extends Serializable> ServiceResultModel<T> success(List<T> list, String msg) {
        List<T> rows = list == null ? Collections.emptyList() : list;
        Long total = list == null ? 0L : list.size();
        return success(msg, msg, rows, total);
    }

    /**
     * 操作成功
     *
     * @param msg
     * @return
     * @param <T>
     */
    public static<T extends Serializable> ServiceResultModel<T> success(String msg) {
        return success(msg, msg, null, null);
    }

    /**
     * 操作成功
     *
     * @return
     * @param <T>
     */
    public static<T extends Serializable> ServiceResultModel<T> success() {
        return success("操作成功", "操作成功", null, null);
    }

    /**
     * 失败返回
     *
     * @param info
     * @param msg
     * @param rows
     * @param total
     * @return
     * @param <T>
     */
    public static<T extends Serializable> ServiceResultModel<T> fail(String info, String msg, List<T> rows, Long total) {
        ServiceResultModel<T> model = new ServiceResultModel<>();
        model.setCode(ResultCodeEnum.ERROR.getCode());
        model.setInfo(info);
        model.setMsg(msg);
        model.setTimestamp(System.currentTimeMillis());
        model.setRows(rows);
        model.setTotal(total);
        return model;
    }

    /**
     * 操作失败
     *
     * @param msg
     * @return
     * @param <T>
     */
    public static<T extends Serializable> ServiceResultModel<T> fail(String msg) {
        return fail(msg, msg, null, null);
    }

}
